<?php

namespace app\model;

use app\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * 部门
 */
class DepartmentModel extends BaseModel
{
    protected $name = 'department';

    /**
     * 新增数据验证
     * @return array
     */
    public function addValidate(): array
    {
        return [
            'unit_id|单位ID'  =>  'require',
            'name|部门名称'  =>  'require',
            'type|学制类型' =>  'require',
        ];
    }

    /**
     * 关联多个用户
     * @return HasMany
     */
    public function users(): HasMany
    {
        return $this->hasMany(UserModel::class,'department_id');
    }

    /**
     * 关联多个部门管理员
     * @return HasMany
     */
    public function departmentAdmins(): HasMany
    {
        return $this->hasMany(DepartmentAdminModel::class,'department_id');
    }

    /**
     * 关联学制模板
     * @return BelongsTo
     */
    public function departmentLevel(): BelongsTo
    {
        return $this->belongsTo(DepartmentLevelModel::class,'mode')->bind(['mode_name' => 'name','level_rules']);
    }

}